package com.easonji.attendance.controller;


import com.easonji.attendance.model.OvertimeState;
import com.easonji.attendance.query.OvertimeVerifyQuery;
import com.easonji.attendance.service.IOvertimeService;
import com.easonji.attendance.service.IOvertimeStateService;
import com.easonji.common.base.controller.BaseController;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.util.StringUtils;
import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;
import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 加班审核 前端控制器
 * </p>
 *
 * @author easonji
 * @since 2018-09-27
 */
@Controller
@RequestMapping("/attendance/overtime/verify")
public class OvertimeVerifyController extends BaseController {


    /**
    * 加班页面前缀
    */
    private final static String PREFIX = "attendance/overtimeverify/";

    @Resource
    private IOvertimeService overtimeService;

    @Resource
    private IOvertimeStateService overtimeStateService;

    /**
    * 跳转到加班首页
    */
    @Permission
    @GetMapping("")
    public String index() {
        return PREFIX + "overtimeverify";
    }

    /**
    * 查询加班列表
    */
    @ResponseBody
    @PostMapping("/list")
    public Object queryList(OvertimeVerifyQuery overtimeVerifyQuery) {
        Object result = overtimeService.queryVerifyList(overtimeVerifyQuery);
        return result;
    }

    /**
    * 跳转到修改加班页面
    */
    @Permission
    @GetMapping("/to_verify/{otId}")
    public String toUpdate(@PathVariable Integer otId, Model model) {
        Object result = overtimeService.queryRowById(otId);
        LogObjectHolder.me().set(result);
        model.addAttribute("overtime", result);
        return PREFIX + "overtimeverify_edit";
    }


    /**
     * 新增加班状态
     */
    @ResponseBody
    @Permission
    @PostMapping("/save")
    public Response save(@Valid OvertimeState overtimeState, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }

        ShiroUser shiroUser = ShiroUtils.getUser();

        OvertimeState overtimeState1 = overtimeStateService.queryOvertimeStateasc(overtimeState.getOtsOvertime());

        if(StringUtils.isEmpty(overtimeState1) || !shiroUser.getUserId().equals(overtimeState1.getOtsAuditor()) ){
            return failure(ResponseEnum.AUDI_01_ERROR);
        }

        overtimeState.setOtsId(overtimeState1.getOtsId());
        overtimeState.setOtsPost(overtimeState1.getOtsPost());


        int result = overtimeStateService.save(overtimeState);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.AUDI_SUCCESS);
    }



}
